//
//  SignInViewController.m
//  MagazineShangejie
//
//  Created by cnbizMedia on 15/12/18.
//  Copyright © 2015年 CnbizMedia. All rights reserved.
//

#import "SignInViewController.h"
#import "Util.h"
@interface SignInViewController (){

    NSString *content;
    NSString *address ;
    NSString *thumb ;
}
@property (weak, nonatomic) IBOutlet UILabel *place;
@property (weak, nonatomic) IBOutlet UILabel *time;
@property (weak, nonatomic) IBOutlet UIButton *ok;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *theTableView;


@end

@implementation SignInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _theTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _theTableView.backgroundColor=UIColorFromRGB(0xf5f5f5);
 
    _theTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

    self.navigationItem.title = @"活动";
    NSArray *singInArray = [UserDefaults objectForKey:@"singInArray"];
    NSString *catid = singInArray[0];
    NSString *bid   = singInArray[1];
    NSDictionary *userDic = [UserDefaults objectForKey:@"user"];
    NSLog(@"singInArray == %@",singInArray);
    NSLog(@"userDic == %@",userDic);
    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"][@"userid"];
//    uid = @"12773";
    
    [[AppAPIClient sharedClient] GET:@"Apply/CheckActive" parameters:@{@"catid":catid,@"id":bid,@"uid":uid} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableArray *trialdata = responseObject[@"data"];
        NSLog(@"responseObject------%@",responseObject);
        NSLog(@"responseObject ==%@",responseObject);
        NSLog(@"trialdata == %@",trialdata);
        
        
        
        NSMutableDictionary *dateDic = trialdata[0];
        thumb = dateDic[@"thumb"];
        address = dateDic[@"address"];
        content = dateDic[@"content"];
        _place.text = dateDic[@"title"];
        
        NSString *timeString = [dateDic objectForKey:@"inputtime"];
        NSLog(@"timeString -- %@",timeString);
        //得到运动时间，需要转成年月日
        NSString *formatString = @"YYYY-MM-dd";
        
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:formatString];
        NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeString intValue] - 28800];
        NSString *dateString =[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:confromTimesp] ];
        NSLog(@"date: %@", dateString);
        _time.text = dateString;
        
        if ([[dateDic objectForKey:@"flag"] integerValue] == 1) {
            _ok.userInteractionEnabled = NO;
            [_ok setTitle:@"已签到" forState:UIControlStateNormal];
        }
        
        [_theTableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"查询订单失败，请稍后在试。"];
    }];
}


- (IBAction)okEvent:(id)sender {
    
    NSArray *singInArray = [UserDefaults objectForKey:@"singInArray"];
    NSString *catid = singInArray[0];
    NSString *bid   = singInArray[1];
    NSDictionary *userDic = [UserDefaults objectForKey:@"user"];
    NSLog(@"singInArray == %@",singInArray);
    NSLog(@"userDic == %@",userDic);
    NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"][@"userid"];
//    uid = @"12773";
    
    [[AppAPIClient sharedClient] GET:@"Apply/CheckIn" parameters:@{@"catid":catid,@"id":bid,@"uid":uid,@"title":_place.text} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *trialdata = responseObject[@"code"];
        NSLog(@"responseObject------%@",responseObject);
        NSLog(@"responseObject ==%@",responseObject);
        NSLog(@"trialdata == %@",trialdata);
        
        if ([trialdata integerValue] == 1 ) {
            _ok.userInteractionEnabled = NO;
            _ok.backgroundColor = [UIColor orangeColor];
            [_ok setTitle:@"已签到" forState:UIControlStateNormal];
        }
//        NSMutableDictionary *dateDic = trialdata[0];
        
        [SVProgressHUD dismiss];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showSuccessWithStatus:@"查询订单失败，请稍后在试。"];
    }];
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    if (section==0) {
////        return [self tableHeader];
//    }else{
//        return nil;
//    }
    return nil;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //#warning Incomplete method implementation.
    //     Return the number of rows in the section.
    
    if (section==0) {
        return 1;
    }
    else if (section==1) {
        return 1;
    }
    else if (section==2) {
        return 1;
    }else if (section==3) {
        return 1;
    }
    return 0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 125;
    }if (indexPath.section==3) {
        if (indexPath.row == 0) {
            _titleLabel.text = content;
            
            [_titleLabel setNumberOfLines:0];
            
            _titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
            
            NSLog(@"heightLabel.text == %@",_titleLabel.text);
            
            
            UIFont *fnt = [UIFont fontWithName:@"HelveticaNeue" size:17.0f];
            _titleLabel.font = fnt;
            
            NSLog(@"1heightLabel.frame.size.height == %f 1heightLabel.frame.size.width == %f",_titleLabel.frame.size.height,_titleLabel.frame.size.width);
            CGSize size = [_titleLabel sizeThatFits:CGSizeMake(300, MAXFLOAT)];
            
            _titleLabel.frame =CGRectMake(_titleLabel.frame.origin.x, _titleLabel.frame.origin.y, _titleLabel.frame.size.width, size.height);
            _titleLabel.font = [UIFont systemFontOfSize:15];
            
            NSLog(@"2heightLabel.frame.size.height == %f 2heightLabel.frame.size.width == %f",_titleLabel.frame.size.height,_titleLabel.frame.size.width);
            int abc = _titleLabel.frame.size.height + 20;// / 20.0f  * 16
            NSLog(@"wordLabel.lineBreakMode %ld",(long)_titleLabel.lineBreakMode);
            
            _titleLabel.hidden = YES;
            
            NSLog(@"abc %d",abc);
            NSLog(@"indexPath.row %ld",(long)indexPath.row);
            return abc;

        }
        return 55;//这里是动态的设置高度的
    }else{
    return 55;
        }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        //
//        return 220;
        return 0;
        //    }else if (section==1){
        //
        //        return 10;
        //    }else if (section==2){
        //
        //        return 10;
        //    }else if (section==3){
        //
        //        return 10;
    }else{
        return 20;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell ;
        if(indexPath.section==0){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        }else if(indexPath.section==3){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        }else if(indexPath.section==2 || indexPath.section==1){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        }
    if (cell==nil) {
        if(indexPath.section==0){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
        }else if(indexPath.section==3){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
        }else if(indexPath.section==2 || indexPath.section==1){
            cell = [tableView dequeueReusableCellWithIdentifier:@"cell3" forIndexPath:indexPath];
        }
    }
    
    UIImageView*imageView=(UIImageView*)[cell.contentView viewWithTag:1];
    UILabel*label=(UILabel*)[cell.contentView viewWithTag:2];
    UILabel*labell=(UILabel*)[cell.contentView viewWithTag:3];
    
    if (indexPath.section==0){
        if (indexPath.row==0) {
            
            imageView.image = [UIImage imageNamed:@"icon_default_ipad.png"];
            
            [imageView sd_setImageWithURL:[NSURL URLWithString:thumb]];
            label.text = _place.text;
            
//            cell.backgroundColor = [UIColor groupTableViewBackgroundColor];
        }}
    
    else if (indexPath.section==1){
 
        
        if (indexPath.row==0) {
            imageView.image=[UIImage imageNamed:@"icon_time"];
            label.text = @"活动时间";
            labell.text = _time.text;
            

            
        }
    }else if (indexPath.section==2){
        
        imageView.image=[UIImage imageNamed:@"icon_address"];
        imageView.hidden = YES;
        UIImageView *addImage = [[UIImageView alloc]initWithFrame:CGRectMake( 18, (55-24)/2, 18, 24)];
        addImage.image=[UIImage imageNamed:@"icon_address"];
        [cell addSubview:addImage];
        label.text=@"活动地址";
        labell.text = address;
        
    }
    else if (indexPath.section==3){
        
        //            imageView.image=[UIImage imageNamed:@"ico17"];
        label.text=@"活动内容";
        labell.text = content;
        
    }
    return cell;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
